﻿using System;
using System.Data;
using System.Text;
using System.Data.OleDb;
using Maticsoft.DBUtility;
namespace Maticsoft
{
    /// <summary>
    /// 类Category。
    /// </summary>
    [Serializable]
    public partial class Categorys
    {
        public Categorys()
        { }
        #region Model
        private int _id;
        private string _tname;
        private string _tcodes;
        private string _tmemo;
        /// <summary>
        /// 
        /// </summary>
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string TNAME
        {
            set { _tname = value; }
            get { return _tname; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string TCODES
        {
            set { _tcodes = value; }
            get { return _tcodes; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string TMEMO
        {
            set { _tmemo = value; }
            get { return _tmemo; }
        }
        #endregion Model


        #region  Method


        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(String Sname, String eId, String code)
        {
            OleDbDataReader dr = null;
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.Append("select TNAME from [Categorys]");
                strSql.Append(" where TNAME=@name ");
                if (eId != "-1")
                {
                    strSql.Append(" and id <> " + eId + "and TCODES='" + code + "'");
                }
                OleDbParameter[] parameters = {
					new OleDbParameter("@name", OleDbType.VarChar,255)};
                parameters[0].Value = Sname;
                dr = DbHelperOleDb.ExecuteReader(strSql.ToString(), parameters);
                return dr.HasRows;
            }
            catch (Exception ex) { return true; }
            finally
            {
                dr.Close();
                dr.Dispose();
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public Boolean Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [Categorys] (");
            strSql.Append("TNAME,TCODES,TMEMO)");
            strSql.Append(" values (");
            strSql.Append("@TNAME,@TCODES,@TMEMO)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@TNAME", OleDbType.VarChar,255),
					new OleDbParameter("@TCODES", OleDbType.VarChar,255),
					new OleDbParameter("@TMEMO", OleDbType.VarChar,255)};
            parameters[0].Value = TNAME;
            parameters[1].Value = TCODES;
            parameters[2].Value = TMEMO;

            return DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters) > 0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [Categorys] set ");
            strSql.Append("TNAME=@TNAME,");
            strSql.Append("TCODES=@TCODES,");
            strSql.Append("TMEMO=@TMEMO");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@TNAME", OleDbType.VarChar,255),
					new OleDbParameter("@TCODES", OleDbType.VarChar,255),
					new OleDbParameter("@TMEMO", OleDbType.VarChar,255),
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = TNAME;
            parameters[1].Value = TCODES;
            parameters[2].Value = TMEMO;
            parameters[3].Value = ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Categorys] ");
            strSql.Append(" where ID=@ID ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@ID", OleDbType.Integer,4)};
            parameters[0].Value = ID;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataTable GetModel(String code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,TNAME,TCODES,TMEMO ");
            strSql.Append(" FROM [Categorys] ");
            strSql.Append(" where TCODES=@TCODES ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@TCODES", OleDbType.VarChar,255)};
            parameters[0].Value = code;

            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds != null && ds.Tables.Count != 0)
            {
                return ds.Tables[0];
            }
            return null;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [Categorys] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }
        public String GetNewsType()
        {
            StringBuilder sbCate = new StringBuilder();
            DataSet ds = GetList("TCODES='news'");
            if (ds != null && ds.Tables.Count != 0)
            {
                sbCate.Append("[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    sbCate.Append("{");
                    sbCate.AppendFormat("\"val\":{0},\"txt\":\"{1}\"", ds.Tables[0].Rows[i]["ID"], ds.Tables[0].Rows[i]["TNAME"]);
                    sbCate.Append("},");
                }
                return sbCate.ToString().Trim(',') + "]";
            }
            return "[]";
        }
        public String GetProductsType()
        {
            StringBuilder sbCate = new StringBuilder();
            DataSet ds = GetList("TCODES='products'");
            if (ds != null && ds.Tables.Count != 0)
            {
                sbCate.Append("[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    sbCate.Append("{");
                    sbCate.AppendFormat("\"val\":{0},\"txt\":\"{1}\"", ds.Tables[0].Rows[i]["ID"], ds.Tables[0].Rows[i]["TNAME"]);
                    sbCate.Append("},");
                }
                return sbCate.ToString().Trim(',') + "]";
            }
            return "[]";
        }
        public String GetList(int page, int rows)
        {
            DataSet ds = GetList("ID>-1");
            if (ds != null && ds.Tables.Count != 0)
            {
                DataTable dt = ds.Tables[0];
                if (dt != null && dt.Rows.Count != 0)
                {
                    DataTable newDt = dt.Clone();
                    int rowCount = dt.Rows.Count - dt.Rows.Count % rows == rows * (page - 1) ? dt.Rows.Count : page * rows;
                    for (int i = rows * (page - 1); i < rowCount; i++)
                    {
                        Object[] dr = dt.Rows[i].ItemArray;
                        newDt.Rows.Add(dr);
                    }
                    return "{\"total\":" + dt.Rows.Count + ",\"rows\":" + DataTableExt.DataTableToJson(newDt).ToString() + "}";
                }
                return "{\"total\":0,\"rows\":[]}";
            }
            return "{\"total\":0,\"rows\":[]}";
        }
        #endregion  Method
    }
}

